﻿using Pb;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NNSettlement : MonoBehaviour {
    EginUser user = EginUser.Instance;
    EginMahjong mahjong = EginMahjong.Instance;
    public List<GameObject> NNSettlementObj;
    public List<UISprite> avatarList;
    public List<UILabel> usernameList;
    public List<UILabel> uidList;
    public List<UITexture> wxAvatarList;

    //scroe
    public List<GameObject> Score;
    public List<GameObject> ScoreFather;
    public List<UISprite> jiajian;
    //rank
    public List<UISprite> rank1;
    public List<UISprite> rankother;

    //bg
    public List<UISprite> ownBg;
    public GameObject BackHall;
    void Awake()
    {
        
        UIEventListener.Get(BackHall).onClick = ButtonClick;
    }

    void ButtonClick(GameObject button)
    {
        if (button.name == "BackHall")
        {
            EginUser.Instance.gameUserInfoList.Clear();
            if (user.gameType == 1) {
                Application.LoadLevel(ScenePath.GameMahjong);
            }
            else if (user.gameType == 2) {
                Application.LoadLevel(ScenePath.GameDN);
            }
            else if (user.gameType == 3)
            {
                Application.LoadLevel(ScenePath.GameDDZ);
            }
            else if (user.gameType == 4)
            {
                //Application.LoadLevel(ScenePath.GameZJH);
            }

        }      
    }

    public  void LoadInfo(List<GameOverUserInfo> gameOverUserInfo) {        
        int tempIndex = 0;
        for (int i=0;i< gameOverUserInfo.Count;i++) {  
            if (gameOverUserInfo[i].TotalRank == 1) {
                LoadUserInfo(gameOverUserInfo, tempIndex, i);
                tempIndex++;
            }
        }
        for (int i = 0; i < gameOverUserInfo.Count; i++)
        {
            if (gameOverUserInfo[i].TotalRank == 2)
            {
                LoadUserInfo(gameOverUserInfo, tempIndex, i);
                tempIndex++;
            }
        }
        for (int i = 0; i < gameOverUserInfo.Count; i++)
        {
            if (gameOverUserInfo[i].TotalRank == 3)
            {
                LoadUserInfo(gameOverUserInfo, tempIndex, i);
                tempIndex++;
            }
        }
        for (int i = 0; i < gameOverUserInfo.Count; i++)
        {
            if (gameOverUserInfo[i].TotalRank == 4)
            {
                LoadUserInfo(gameOverUserInfo, tempIndex, i);
                tempIndex++;
            }
        }
        for (int i = 0; i < gameOverUserInfo.Count; i++)
        {
            if (gameOverUserInfo[i].TotalRank == 5)
            {
                LoadUserInfo(gameOverUserInfo, tempIndex, i);
                tempIndex++;
            }
        }
    }

    void LoadUserInfo(List<GameOverUserInfo> gameOverUserInfo,int tempIndex ,int i) {

        #region
        NNSettlementObj[tempIndex].SetActive(true);
        int tempSeatID = gameOverUserInfo[i].SeatId;
        if (user.gameType == 1)
        {
            for (int j = 0; j < user.gameUserInfoList.Count;j++ )
            {
                if(int.Parse(uidList[tempIndex].text) == user.gameUserInfoList[i].Uid){
                    if (user.gameUserInfoList[j].AvatarUrl != "")
                    {
                        WXAvatarManager._instance.GetWxAvatar(mahjong.Otheruid[tempSeatID], wxAvatarList[tempIndex], user.gameUserInfoList[j].AvatarUrl);
                        wxAvatarList[tempIndex].gameObject.SetActive(true);
                    }
                }
            }
            
            avatarList[tempIndex].spriteName = mahjong.OtherAvatarid[tempSeatID].ToString();
                
            //avatarList[tempIndex].spriteName = mahjong.OtherAvatarid[tempSeatID].ToString();
            usernameList[tempIndex].text = mahjong.OtherNickName[tempSeatID];
            uidList[tempIndex].text = mahjong.Otheruid[tempSeatID].ToString();
            //StartCoroutine(GetWWWFACE(wxAvatarList[i],));
        }
        else {
            for (int j = 0; j < user.gameUserInfoList.Count; j++)
            {
                if (tempSeatID == user.gameUserInfoList[j].SeatId)
                {
                    if(user.gameUserInfoList[j].AvatarUrl != ""){
                        WXAvatarManager._instance.GetWxAvatar(user.gameUserInfoList[j].Uid,wxAvatarList[tempIndex],user.gameUserInfoList[j].AvatarUrl);
                        wxAvatarList[tempIndex].gameObject.SetActive(true);
                    }
                    else
                    {
                        avatarList[tempIndex].spriteName = user.gameUserInfoList[j].AvatarId.ToString();
                    }                  
                    usernameList[tempIndex].text = user.gameUserInfoList[j].Nickname;
                    uidList[tempIndex].text = user.gameUserInfoList[j].Uid.ToString();
                }
            }
        }
        
        int tempScore = gameOverUserInfo[i].TotalScore;
        
        string tempStr = user.ReturnRelCoin(Mathf.Abs(gameOverUserInfo[i].TotalScore));
        Debug.Log(tempStr);
        float x = Score[tempIndex].transform.localPosition.x;
        float y = Score[tempIndex].transform.localPosition.y;
        if (tempScore >= 0)
        {
            jiajian[tempIndex].gameObject.SetActive(true);
            jiajian[tempIndex].spriteName = "plus_" + "jia";
            int len = tempStr.Length;
            for (int j = 0; j < len; j++)
            {
                GameObject obj = (GameObject)Instantiate(Score[tempIndex]);
                obj.SetActive(true);
                obj.transform.parent = ScoreFather[tempIndex].transform;
                obj.transform.localScale = new Vector3(1, 1, 1);
                string str = tempStr.Substring(0, 1);
                if (j == 0)
                {
                    obj.transform.localPosition = new Vector3(x, y, 0);
                }
                else
                {
                    if (str == ".")
                    {
                        x += 26;

                    }
                    else if (str == "w")
                    {
                        x += 50;
                    }
                    else
                    {
                        x += 40;
                    }
                    obj.transform.localPosition = new Vector3(x, y, 0);
                }
                tempStr = tempStr.Remove(0, 1);
                obj.GetComponent<UISprite>().spriteName = "plus_" + str;
            }
        }
        else
        {
            jiajian[tempIndex].gameObject.SetActive(true);
            jiajian[tempIndex].spriteName = "less_" + "jian";
            int len = tempStr.Length;
            for (int j = 0; j < len; j++)
            {
                GameObject obj = (GameObject)Instantiate(Score[tempIndex]);
                obj.SetActive(true);
                obj.transform.parent = ScoreFather[tempIndex].transform;
                obj.transform.localScale = new Vector3(1, 1, 1);
                string str = tempStr.Substring(0, 1);
                if (j == 0)
                {
                    obj.transform.localPosition = new Vector3(x, y, 0);
                }
                else
                {
                    if (str == ".")
                    {
                        x += 26;
                    }
                    else if (str == "w")
                    {
                        x += 50;
                    }
                    else
                    {
                        x += 40;
                    }
                    obj.transform.localPosition = new Vector3(x, y, 0);
                }
                tempStr = tempStr.Remove(0, 1);
                obj.GetComponent<UISprite>().spriteName = "less_" + str;
            }
        }

        #endregion

        int rank = gameOverUserInfo[i].TotalRank;
        if (rank == 1)
        {
            rank1[tempIndex].gameObject.SetActive(true);
        }
        else {
            rankother[tempIndex].gameObject.SetActive(true);
            rankother[tempIndex].spriteName = rank.ToString();
        }
        int sid = 0;
        if (user.gameType == 1)
        {
            sid = EginMahjong.Instance.seatID;
        }
        else if (user.gameType == 2)
        {
            sid = EginDN.Instance.seatId;
        }
        else if (user.gameType == 3)
        {
            sid = EginDDZ.Instance.seatId;
        }
        else if (user.gameType == 4)
        {
            sid = EginPDK.Instance.seatId;
        }
        if (gameOverUserInfo[i].SeatId == sid) {

            ownBg[tempIndex].spriteName = "zijibg";
        }
    }

    /// <summary>
    /// 加载微信头像
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    //IEnumerator GetWWWFACE(UITexture texture, string url)
    //{

    //    if (url != "")
    //    {
    //        WWW www = new WWW(url);


    //        yield return www;
    //        if (www.isDone)
    //        {
    //            if (www.error == null) { }
    //            texture.mainTexture = www.texture;

    //        }
    //        else
    //        {

    //        }
    //        www.Dispose();
    //    }

    //}   
}
